<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: client/xhr.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: client/xhr.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>
/**
 * XHR请求处理模块
 * @module XHR
 */
import Response from '../response';
import { isNoDataMethod } from '../utils';

export default function (request) {
  return new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest();
    xhr.open(request.method.toUpperCase(), request.url, true);
    xhr.timeout = request.getClientOption('timeout');

    if (request.crossDomain &amp;&amp; request.inBrowser &amp;&amp; !('withCredentials' in xhr)) {
      xhr = new window.XDomainRequest(); // IE8/9 不支持跨域
    }

    // set header
    request.header.forEach((k, v) => {
      xhr.setRequestHeader(k, v);
    });

    // responseType
    if (request.getClientOption('responseType') &amp;&amp; 'responseType' in xhr) {
      xhr.responseType = request.responseType;
    }
    // withCredentials
    if (request.getClientOption('withCredentials')) {
      xhr.withCredentials = true;
    }

    // 设置监听事件
    const onprogress = request.getClientOption('onProgress');
    if (onprogress) {
      if (request.method === 'get') {
        xhr.addEventListener('progress', onprogress);
      } else if (request.method === 'put' || request.method === 'post') {
        xhr.upload.addEventListener('progress', onprogress);
      }
    }

    xhr.onload = function handler() {
      // IE sends 1223 instead of 204
      const status = xhr.status === 1223 ? 204 : xhr.status;
      const statusText = xhr.statusText === 1223 ? 'No Content' : xhr.statusText;
      const successCheck = request.getClientOption('successCheck');
      if (!successCheck) throw new Error('no successCheck function');
      if (!successCheck(status)) {
        reject(new Error(`error status: ${status}; status text: ${statusText}`));
      }
      const res = new Response(xhr.response || xhr.responseText, status, statusText, request);
      resolve(res);
      xhr = null;
    };

    xhr.ontimeout = function timeoutHandler() {
      reject(new Error('timeout!'));
      xhr = null;
    };

    xhr.onerror = function errorHandler(err) {
      reject(err);
      xhr = null;
    };
    xhr.onabort = function abortHandler() {
      reject(new Error('cancel request'));
      xhr = null;
    };
    if (isNoDataMethod(request.method) &amp;&amp; request.data !== null) {
      throw Error('the method such as get, header, options, delete, jsonp can not have message body');
    }
    xhr.send(request.data);
  });
}

</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-Core.html">Core</a></li><li><a href="module-JSONP.html">JSONP</a></li><li><a href="module-Utils.html">Utils</a></li><li><a href="module-XHR.html">XHR</a></li></ul><h3>Classes</h3><ul><li><a href="Header.html">Header</a></li><li><a href="InterceptorFactory.html">InterceptorFactory</a></li><li><a href="module-Core-Q.html">Q</a></li><li><a href="Request.html">Request</a></li><li><a href="Response.html">Response</a></li></ul><h3>Namespaces</h3><ul><li><a href="q.html">q</a></li></ul><h3>Global</h3><ul><li><a href="global.html#contentInterceptor">contentInterceptor</a></li><li><a href="global.html#defaultOptions">defaultOptions</a></li><li><a href="global.html#xrfsInterceptor">xrfsInterceptor</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Apr 18 2017 23:31:24 GMT+0800 (CST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
